//
//  VideoStyleViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/21.
//

import UIKit

class VideoStyleViewController: BaseViewController {
    let viewModel = VideoViewModel()
    var titleStr:String = ""
    var message:String = ""
    let disposeBag = DisposeBag()
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(cellWithClass: VideoStyleCell.self)
        tableView.register(cellWithClass: PaintingScaleCell.self)
        tableView.register(cellWithClass: VideoAudioTableViewCell.self)
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorStyle = .none
        tableView.backgroundColor = .clear
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 72, right: 0)
        return tableView
    }()
    private lazy var cancelBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleForNormal = "取消"
        button.titleFont = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = .white
        button.titleColorForNormal = Theme.color.text_d1
        button.allRound(24)
        return button
    }()
    
    private lazy var nextBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleForNormal = "下一步"
        button.titleFont = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#25262B")
        button.allRound(24)
        button.titleColorForNormal = .white
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        self.navigationItem.title = "视频生成"
        self.view.addSubview(tableView)
        self.view.addSubview(self.cancelBtn)
        self.view.addSubview(self.nextBtn)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(self.safeBottom)
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.bottom.equalTo(self.safeBottom).offset(-10)
            make.leading.equalToSuperview().offset(15)
            make.height.equalTo(48)
            // 设置宽度等于 nextBtn 的宽度
            make.width.equalTo(nextBtn.snp.width)
        }

        nextBtn.snp.makeConstraints { make in
            make.bottom.equalTo(self.safeBottom).offset(-10)
            // 设置 leading 等于 cancelBtn 的 trailing 加上 14 的间隔
            make.leading.equalTo(cancelBtn.snp.trailing).offset(14)
            make.trailing.equalToSuperview().offset(-15)
            make.height.equalTo(48)
        }

        nextBtn.setClosureTouchUpInside_k { [weak self] in
            guard let self = self else { return }
            self.viewModel.create_video(title: self.titleStr, message: self.message)
        }
        
        cancelBtn.setClosureTouchUpInside_k { [weak self] in
            self?.popSelf(animation: true)
        }
        
  
        
        self.viewModel.getStyle()
        self.viewModel.getAudio()
        
        self.viewModel.reloadDataSubject.subscribe(onNext: { [weak self] in
            self?.tableView.reloadData()
        }).disposed(by: disposeBag)
        
        self.viewModel.resultIdSubject.subscribe(onNext: { [weak self] id in
            let vc = VideoGeneratingVC()
            vc.ids = id
            self?.navigationController?.pushViewController(vc)
        }).disposed(by: disposeBag)
    }
}
extension VideoStyleViewController: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withClass: VideoStyleCell.self, for: indexPath)
            cell.setup(model: viewModel.listModel?.style)
            cell.chooseStyleBag = {[weak self] id in
                self?.viewModel.style_id = id
            }
            return cell
        }else if indexPath.row == 1{
            let cell = tableView.dequeueReusableCell(withClass: PaintingScaleCell.self, for: indexPath)
            cell.setup(model: viewModel.listModel?.proportion)
            cell.choosePBag = {[weak self] id in
                self?.viewModel.proportion_id = id
            }
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withClass: VideoAudioTableViewCell.self, for: indexPath)
            if let model = self.viewModel.selectVoiceModel {
                cell.sgLb.text = "\(model.title ?? "")-\(model.theme ?? "")"
                cell.iconIV.image = UIImage(named: "icon_login_a")
            }
            return cell
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 2 {
            let popView = VideoAudioPOPView()
            popView.setup(models: self.viewModel.voiceArr)
            popView.show()
            popView.chooseVoice = { [weak self] model in
                guard let self = self else { return }
                for i in 0..<self.viewModel.voiceArr.count {
                        self.viewModel.voiceArr[i].isSelect = false
                }
                if let index = self.viewModel.voiceArr.firstIndex(where: { $0.id == model.id ?? 0}) {
                    self.viewModel.voiceArr[index].isSelect = true
                    self.viewModel.selectVoiceModel = self.viewModel.voiceArr[index]
                }
                self.tableView.reloadData()
            }
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            let height = (SCREEN_WIDTH - 56) / 4
            return 50 + height
        }else if indexPath.row == 1 {
            let height = (SCREEN_WIDTH - 56) / 4
            return height + 66
        }else {
            return 102
        }
    }
}
